<?php

include_once $babInstallPath . 'utilit/calincl.php';
include_once $babInstallPath . 'utilit/mcalincl.php';
include_once $babInstallPath . 'utilit/uiutil.php';
include_once $babInstallPath . 'utilit/evtincl.php';

function unsCalendar_addLecture($materialID, $lectureType, $formationID, $tutorialGroupID, $workPraticeGroupID, $classroomID, $startDate, $startHour, $startMin, $lengthHour, $lengthMin, $repeat) {

    $workPraticeGroups = array();
    $color = null;


    if ($lectureType == 1) {
        
    } else if ($lectureType == 2) {
        $workPraticeGroups = unsDepartement_getWorkPraticeGroupsForTutorialGroup($tutorialGroupID);
        $color = "#808080";
    } else if ($lectureType == 3) {
        $workPraticeGroups[0] = unsDepartement_getWorkPraticeGroup($workPraticeGroupID);
        $color = "#808080";
    }

    $classroom = unsCalendar_getClassroom($classroomID);



    $material = unsDepartement_getMaterial($materialID);
    $lectureT = unsCalendar_getLectureType($lectureType);

    $title =  $lectureT['acronym']. ' ' . $material['name'];
    $description = 'Cours de ' . $material['name'];
    $description_format = "html";
    $location = $classroom['name'];
    $start_date = $startDate . ' ' . $startHour . ':' . $startMin . ':00';

    $endMin = ($startMin + $lengthMin) % 60;
    $endHour = floor(($startMin + $lengthMin) / 60) + $startHour + $lengthHour;
    $end_date = $startDate . ' ' . $endHour . ':' . $endMin . ':00';

    $id_cat = 0;
    $id_creator = $GLOBALS['BAB_SESS_USERID'];
    $id_modifiedby = $GLOBALS['BAB_SESS_USERID'];
    $hash = 'null';



    $bprivate = "N";
    $block = "Y";
    $bfree = "N";
    $date_modification = "now()";
    $parent_calendar = "resource/" . $classroom['calendarID'];
    $uuid = unsCalendar_generateUID();

    global $babDB, $babBody;

    $classroomDispo = unsCalendar_calendarIsDisponible($classroom['calendarID'], $start_date, $end_date);
    if ($classroomDispo == 0) {
        $professorCalendar = unsCalendar_staffCalendar($material['professorID']);
        $professorDispo = unsCalendar_calendarIsDisponible($professorCalendar['id'], $start_date, $end_date);
        if ($professorDispo == 0) {
            $i = 0;
            foreach ($workPraticeGroups as $workPraticeGroup) {
                $wpgCalendar = unsCalendar_getCalendarForWorkPraticeGroup($workPraticeGroup['id']);
                $i+=unsCalendar_calendarIsDisponible($wpgCalendar['calendarID'], $start_Date, $end_Date);
            }
            if ($i == 0) {
                $babDB->db_query("insert into " . BAB_CAL_EVENTS_TBL . " (title, description, description_format, location, start_date, end_date, id_cat, id_creator, hash, color, bprivate, block, bfree, date_modification, id_modifiedby, uuid, parent_calendar) values ('$title', '$description', '$description_format', '$location', '$start_date', '$end_date', $id_cat, '$id_creator', '$hash', '$color', '$bprivate', '$block', '$bfree', $date_modification, $id_modifiedby, '$uuid', '$parent_calendar');");
                $idEvent = $babDB->db_insert_id();
                $babDB->db_query("insert into " . BAB_CAL_EVENTS_OWNERS_TBL . "(id_event, calendar_backend,caltype,id_cal,status, idfai) values ($idEvent, 'Ovi', 'resource', " . $classroom['calendarID'] . ", 0, 0);");
                $babDB->db_query("insert into " . BAB_CAL_EVENTS_OWNERS_TBL . "(id_event, calendar_backend,caltype,id_cal,status, idfai) values ($idEvent, 'Ovi', 'personal', " . $professorCalendar['id'] . ", 0, 0);");

                $babDB->db_query("INSERT INTO ".UNSCALENDAR_LECTURES_TABLE_NAME." (lecture_formationID, 
                lecture_tutorialGroupID, 
                lecture_workPraticeGroupID, 
                lecture_eventID, 
                lecture_classroomID,
                lecture_materialID,
                lecture_lectureTypeID,
                lecture_startDate,
                lecture_lengthHour, 
                lecture_lengthMin,
                lecture_datetime,
                lecture_author)
                VALUES ('$formationID', '$tutorialGroupID', '$workPraticeGroupID',  '$idEvent',  '$classroomID', '$materialID', '$lectureType','$start_date', $lengthHour, $lengthMin , now(), $id_creator)" );
                
                foreach ($workPraticeGroups as $workPraticeGroup) {
                    $wpgCalendar = unsCalendar_getCalendarForWorkPraticeGroup($workPraticeGroup['id']);
                    $babDB->db_query("insert into " . BAB_CAL_EVENTS_OWNERS_TBL . "(id_event, calendar_backend,caltype,id_cal,status, idfai) values ($idEvent, 'Ovi', 'public', " . $wpgCalendar['calendarID'] . ", 0, 0);");
                }
            } else {
                $babBody->addError(bab_translate("A group have already a lecture !", $GLOBALS['babAddonFolder']));
            }
        } else {
            $babBody->addError(bab_translate("Professor not available !", $GLOBALS['babAddonFolder']));
        }
    } else {
        $babBody->addError(bab_translate("Classroom not available !", $GLOBALS['babAddonFolder']));
    }
    
    
}

function unsCalendar_getLecturesForFormation($id)
{
    global $babDB;


    $res = $babDB->db_query("select * from " . UNSCALENDAR_LECTURES_TABLE_NAME . " where lecture_formationID =".$id." order by lecture_startDate");

    $lectures = array();
    $i = 0;
    while ($arr = $babDB->db_fetch_array($res)) {
        $lectures[$i] = $arr;
        $i++;
    }
    return $lectures;
}

?>
